{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "e0669e97",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from sqlalchemy import create_engine"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "6879e5f7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Engine(mysql+pymysql://root:***@127.0.0.1:3306/testdb?charset=utf8)\n"
     ]
    }
   ],
   "source": [
    "# 创建一个MySQL连接器，用户名为root，密码为1234\n",
    "# 地址为127.0.0.1，数据库名称为testdb，编码为UTF-8\n",
    "engine = create_engine('mysql+pymysql://root:123456@127.0.0.1:3306/testdb?charset=utf8')\n",
    "print(engine)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "0ef79bc2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "testdb数据库数据表清单为:\n",
      "      Tables_in_testdb\n",
      "0            employee\n",
      "1                 job\n",
      "2                job2\n",
      "3                jobs\n",
      "4  meal_order_detail1\n",
      "5           musicdata\n"
     ]
    }
   ],
   "source": [
    "# 代码3-7\n",
    "# 使用read_sql_query函数查看testdb中的数据表数目\n",
    "musicadatalist = pd.read_sql_query('show tables', con=engine)\n",
    "print('testdb数据库数据表清单为:\\n', musicadatalist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "9f5a2f5a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "使用read_sql_table函数读取音乐行业收入信息表的长度为:\n",
      " 3008\n"
     ]
    }
   ],
   "source": [
    "# 使用read_sql_table函数读取音乐行业收入信息表\n",
    "musicdata = pd.read_sql_table('musicdata', con=engine)\n",
    "print('使用read_sql_table函数读取音乐行业收入信息表的长度为:\\n', len(musicdata))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "838b1c74",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "pandas.core.frame.DataFrame"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(musicdata)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f27ddb78",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
